package org.mogu;


import org.mogu.adapter.ContactListAdapter;
import org.mogu.adapter.FriendExpandableListAdapter;
import org.mogu.control.util.FriendExpandableChildClickListener;
import org.mogu.control.util.QQAdapterOnItemClickListener;
import org.mogu.staticvalue.Convert;


import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;

public class ContactListActivity extends Activity {
	/*
	 * 相关变量定义
	 * */
	public static ContactListActivity instance = null;
	
//	private Workspace mFrameLayout  = null;//自定义的显示好友界面的FrameLayout控件
	private ListView mRecentList = null;
	private ListView mGroupList = null;
	private  ExpandableListView mfriendList = null;
	
//	private ArrayList arrayFriendList = null;
//	private ArrayList localArrayList2 = null;
	
	//好友列表数据源
	private FriendExpandableListAdapter mFriendExpandableListAdapter = null;
	private FriendExpandableChildClickListener mFriendExpandableChildClickListener = null;
//	private FriendExpandableGroupListener mFriendExpandableGroupListener = null;
//	private FriendExpandableGroupCollapseListener mFriendExpandableGroupCollapseListener = null;
	
	
	//最近联系数据源
	private ContactListAdapter mContactListAdapter = null;
//	private GroupListAdapter mGroupListAdapter = null;
	
	
    //ListView控件数据项单击事件监听器
	private QQAdapterOnItemClickListener mAdapterOnItemClickListener = null;
	
//	private  Vector treeFriendList = null;
	
	//登录对话框中菜单对话框
//	private Qq_MenuDialog mQq_MenuDialog = null;
//	private int menuState = -1;//用于去除多余的Menu事件
	
	private String code=null;
	
	
	public ContactListActivity(){
//		arrayFriendList = new ArrayList();
//		localArrayList2 = new ArrayList();
		
		mFriendExpandableChildClickListener = new FriendExpandableChildClickListener(this);
//		mFriendExpandableGroupListener = new FriendExpandableGroupListener(this);
//		mFriendExpandableGroupCollapseListener = new FriendExpandableGroupCollapseListener(this);
	
//		treeFriendList = new Vector();
	}
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//启用窗体的扩展特性,此处请求为没有标题的activity
        setContentView(R.layout.contact_list);//设置当前使用的布局文件
        
        instance = this;
        Intent intent=this.getIntent();  
        Bundle bunde = intent.getExtras();
		code = bunde.getString("code");
		
        /*
         * 控件初始化
         * */
//        mFrameLayout = (Workspace)findViewById(R.id.Workspace);//获取显示好友界面的FrameLayout控件
        
        /*
         * 好友列表初始化
         * */
        mfriendList = (ExpandableListView)findViewById(R.id.contactlist);
        mfriendList.setGroupIndicator(null);
        Init_FriendList();
        mfriendList.setVisibility(View.VISIBLE);
        
        /*
         * 最近联系初始化
         * */
        mRecentList = (ListView)findViewById(R.id.recentlist);
        mRecentList.setVisibility(View.INVISIBLE);
        mRecentList.setSelector(R.drawable.trans);
        mRecentList.requestFocusFromTouch();
       
        
        /*
         * 好友群初始化
         * */
        mGroupList = (ListView)findViewById(R.id.grouplist);
        mGroupList.setVisibility(View.INVISIBLE);
        mGroupList.setSelector(R.drawable.trans);
        mGroupList.requestFocusFromTouch();
        
        
        
        
        /*
         * 创建ListView控件数据项单击事件监听器
         * */
        mAdapterOnItemClickListener = new QQAdapterOnItemClickListener(this);
        
        if(Convert.IM_Init_tab==0){
        	selectRecentList();
        }
        if(Convert.IM_Init_tab==1){
        	selectFriend();
        }
    }
    
    /*
     * 二级视图里列表的单击事件
     * */
    public void onChildClick(Bundle bundle){
    	Intent mIntent = new Intent(this, ChatWindowsActivity.class);
    	mIntent.putExtras(bundle);
//    	mIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
    	startActivityForResult(mIntent, 0);
    }
    
    
    /*
     * 
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(mFriendExpandableListAdapter!=null){
	    	mFriendExpandableListAdapter.upVar();
	    	Init_FriendList();
	    }
		if(mContactListAdapter!=null){
			mContactListAdapter.upVar();
		}
		
    }
    
    private void selectFriend(){
    	mfriendList.setVisibility(View.VISIBLE);
    	mRecentList.setVisibility(View.INVISIBLE);
        mGroupList.setVisibility(View.INVISIBLE);
        if(mFriendExpandableListAdapter!=null){
        	mFriendExpandableListAdapter.upVar();
        }
    }
    
    public void selectRecentList(){
    	mRecentList.setVisibility(View.VISIBLE);
    	mfriendList.setVisibility(View.INVISIBLE);
        mGroupList.setVisibility(View.INVISIBLE);
        
        	mContactListAdapter = new ContactListAdapter(this,code);
        	mRecentList.setAdapter(mContactListAdapter);
        	//设置ListView控件数据项单击事件监听器
        	mRecentList.setOnItemClickListener(mAdapterOnItemClickListener);
    }
    
    /*
     * 切换显示的内容视图--最近联系，好友列表，好友群三部分
     * */
    public void changeView(View paramView){
    	 
	    switch(paramView.getId()){
		    //最近联系控件
		    case R.id.ImageView01:
		    	selectRecentList();
		        break;
		    
		    //好友列表控件
		    case R.id.ImageView02:
		    	selectFriend();
//		        Init_FriendList();
		        break;
		    	
		    //好友群控件
		    case R.id.ImageView03:
		    	mGroupList.setVisibility(View.VISIBLE);
		    	mfriendList.setVisibility(View.INVISIBLE);
		    	mRecentList.setVisibility(View.INVISIBLE);
		    	
//		    	if(mGroupListAdapter == null){
//		    		mGroupListAdapter = new GroupListAdapter(this);
//		    		mGroupList.setAdapter(mGroupListAdapter);
//		    		//设置ListView控件数据项单击事件监听器
//		    		mGroupList.setOnItemClickListener(mAdapterOnItemClickListener);
//		    	}
		    	break;
	    }
	    
    }
    
    private void Init_FriendList(){
    	mFriendExpandableListAdapter = new FriendExpandableListAdapter(this,code);
		mfriendList.removeAllViewsInLayout();
    	mfriendList.setAdapter(mFriendExpandableListAdapter);
    	mfriendList.requestFocusFromTouch();
    	mfriendList.requestFocus();
//    	mfriendList.collapseGroup(1);
    	mfriendList.expandGroup(0);
    	mfriendList.expandGroup(1);
//    	
    	mfriendList.setOnChildClickListener(mFriendExpandableChildClickListener);
//    	mfriendList.setOnGroupExpandListener(mFriendExpandableGroupListener);
//    	mfriendList.setOnGroupCollapseListener(mFriendExpandableGroupCollapseListener);
//    	
    	Drawable localDrawable = getResources().getDrawable(R.drawable.trans);
    	mfriendList.setSelector(localDrawable);
    	
    }
    
    
    /*
     * dispatchKeyEvent是做分发的工作,具体流程如下：
     * 当键盘按下时 
	 * 首先触发dispatchKeyEvent
	 * 然后触发onUserInteraction
	 * 再次onKeyDown
     * 如果按下紧接着松开，则是俩步
     * 紧跟着触发dispatchKeyEvent
     * 然后触发onUserInteraction
     * 再次onKeyUp
     * */
    public boolean dispatchKeyEvent(KeyEvent event){
    	System.out.println("dispatchKeyEvent");
		
    	try {
    		return super.dispatchKeyEvent(event);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		return false;
    }
    
    
  
    /*
     * 继承重写创建菜单函数
     * 此函数只在第一次按Menu键显示Menu时显示
     * 
     * */
    @Override
    public boolean onCreateOptionsMenu(Menu paramMenu){
    	System.out.println("ContactListActivity onCreateOptionsMenu");
    	
    	//调用父类创建菜单函数
    	return super.onCreateOptionsMenu(paramMenu);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
    		return super.onKeyDown(keyCode, event);
    }
    
    
    /*
     * 继承重写了菜单显示前准备函数
     * 此函数在每次显示菜单时都会执行
     * */
    @Override
    public boolean onPrepareOptionsMenu(Menu paramMenu){
    	System.out.println("ContactListActivity onPrepareOptionsMenu");
    	
    	/*
    	 * 对话框的处理
    	 * */
//    	if(mQq_MenuDialog != null){
//    		if(mQq_MenuDialog.isShowing()){
//    			mQq_MenuDialog.dismiss();
//    		}else{
//    			mQq_MenuDialog.show();
//    		}
//    		
//    	}else{
//    		
//    		//加载Menu的布局文件，注意此处使用的是getMenuInflater，而加载布局文件时使用的是getLayoutInflater
//    		getMenuInflater().inflate(R.menu.qmain_menu_list, paramMenu);
//    		//创建对话框对象
//    		mQq_MenuDialog = new Qq_MenuDialog(this,paramMenu,R.style.dialog);
//    		//对话框添加事件监听器，这里主要实现当Menu显示时，再次按Menu时取消显示
//    		mQq_MenuDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
//    			@Override
//    			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//    				System.out.println("keyCode = "+keyCode);	
//    				if(keyCode == event.KEYCODE_BACK | keyCode == event.KEYCODE_MENU){
//    					if(menuState == 1){
//    						if(mQq_MenuDialog.isShowing()){
//    							mQq_MenuDialog.dismiss();
//    						}
//    					}else{
//    						menuState = 1;
//    					}
//    				}
//    				return true;
//    			}
//    		});
//    		//监听对话框取消显示事件
//    		mQq_MenuDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//				
//				@Override
//				public void onDismiss(DialogInterface dialog) {
//					// TODO Auto-generated method stub
//					menuState = -1;
//				}
//			});
//    			
//    		//显示对话框
//    		mQq_MenuDialog.show();
//    	}
    	
    	return super.onPrepareOptionsMenu(paramMenu);
    	
    	
    }
    
    
    /*
     * 继承重写了菜单选择函数
     * 此函数在每次选择菜单项时都会执行此函数
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem paramMenuItem){
    	//根据ID来判断当前选择的菜单 
    	switch (paramMenuItem.getItemId()){
	    		
	    	
    	}
    	return true;
    }
    
    public void onResume() {
	    super.onResume();
	    if(mFriendExpandableListAdapter!=null){
	    	mFriendExpandableListAdapter.getData();
	    }
	    
	}
	public void onPause() {
	    super.onPause();
		if(mFriendExpandableListAdapter!=null){
	    	mFriendExpandableListAdapter.upVar();
	    }
		if(mContactListAdapter!=null){
			mContactListAdapter.upVar();
		}
	}
	
}
